package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.SalesTop10ItemDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderStatusCountVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrdersMapper {
    public int insert(Orders order);

    void update(Orders orders);

    @Select("select * from orders where number = #{orderNumber}")
    Orders selectByOrderNumber(String orderNumber);

    Page<Orders> selectByUserIdStatus(Long userId, Integer status);

    @Select("select * from orders where id = #{id}")
    Orders selectById(@Param("id")Long id);

    Page<Orders> search(OrdersPageQueryDTO dto);

    OrderStatisticsVO statistics();

    @Select("select * from orders where status = #{status} and order_time < #{oneHourBefore}")
    Page<Orders> selectByStatusAndOrderTimeBefore(@Param("status") Integer status, LocalDateTime oneHourBefore);

    @Select("select ifnull(sum(amount),0) from orders where delivery_time between #{startTime} and #{endTime} and status = #{status}")
    double sumByStatusAndDeliveryTime(LocalDateTime startTime, LocalDateTime endTime, Integer status);

    int countByStatusAndCreateTime(Integer status, LocalDateTime startTime, LocalDateTime endTime);

    List<SalesTop10ItemDTO> saleTop10(LocalDateTime startTime, LocalDateTime endTime);
    @Select("select ifnull(sum(amount),0) from orders where order_time between #{start} and #{end} and status = #{status}")
    Double sumByStatusAndCreateTime(Integer status, LocalDateTime start, LocalDateTime end);

    @Select("select status , count(*) `count` " +
            " from orders " +
            " group by `status`")
    List<OrderStatusCountVO> groupCountByStatusAndCreateTime(LocalDateTime start, LocalDateTime end);
}
